﻿namespace WebSilverlight.Views
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.ServiceModel.DomainServices.Client.ApplicationServices;
    using System.ServiceModel.DomainServices.Client;
    using System.Windows;
    using System.Windows.Controls;
    using WebSilverlight.Web;

    public partial class ClienteEditFlip : ChildWindow
    {
        #region Properties

            private IList<OperationBase> possiblyPendingOperations = new List<OperationBase>();

            private ClienteWrapper _Current;
            public ClienteWrapper Current {
                get { return _Current; }
                set { 
                    _Current = value;
                    this.Edit1.Current = value;
                    this.Edit2.Current = value;
                }
            }
            public ClienteWrapper Original { get; set; }
            public bool IsNew { get; set; }
            public bool IsEditingFirstState { get; set; }
            public ClienteDomainContext DomainContext { get; set; }
            public DomainDataSource DDS { get; set; }

        #endregion

        public ClienteEditFlip()
        {
            ClienteInit(false);
        }
        public ClienteEditFlip(bool IsEditing)
        {
            ClienteInit(IsEditing);
        }

        private void ClienteInit(bool IsEditing)
        {
            InitializeComponent();

            IsEditingFirstState = IsEditing;
            IsNew = false;
            //GetCurrent();

            this.Edit1.SetParentWindow(this);
            this.Edit2.SetParentWindow(this);

            this.LayoutUpdated += this.GoToInitialState;
            this.LayoutUpdated += this.UpdateTitle;     
        }

        private void GoToInitialState(object sender, EventArgs eventArgs)
        {
            if(IsEditingFirstState)
                VisualStateManager.GoToState(this, "AtFirstChild", false);
            else
                VisualStateManager.GoToState(this, "AtSecondChild", false);
            this.LayoutUpdated -= this.GoToInitialState;
        }
        private void UpdateTitle(object sender, EventArgs eventArgs)
        {
            //string edit = string.Format(ApplicationStrings.EditWindowEditingTitle, "Cliente");
            //string view = string.Format(ApplicationStrings.EditWindowDisplayTitle, "Cliente");
                        
            this.Title = (this.Edit2.Visibility == Visibility.Visible) ?
                "Edit" : "Cliente";

        }
        public void AddPendingOperation(OperationBase operation)
        {
            this.possiblyPendingOperations.Add(operation);
        }
        public virtual void NavigateToEdit()
        {
            VisualStateManager.GoToState(this, "AtFirstChild", true);
        }
        public virtual void NavigateToView()
        {
            VisualStateManager.GoToState(this, "AtSecondChild", true);
        }
        public virtual void NavigateToEnd()
        {   
            Close();
        }       

        private void Window_Closing(object sender, CancelEventArgs eventArgs)
        {            
            foreach (OperationBase operation in this.possiblyPendingOperations)
            {
                if (!operation.IsComplete)
                {
                    if (operation.CanCancel)
                    {
                        operation.Cancel();
                    }
                    else
                    {
                        eventArgs.Cancel = true;
                    }
                }
            }
        }
    }
}